package com.neusoft.elmboot.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.neusoft.elmboot.po.Point;
import com.neusoft.elmboot.po.PointRollover;

@Mapper
public interface PointMapper {
	
	@Insert("insert into points value(null,#{userId},#{orderId},#{point},now(),date_add(now(),interval 1 year))")
	int savePoint(String userId,int orderId,int point);
	
	@Select("select max(pointId) from points where userId=#{userId}")
	int getLastPointIdByUserId(String userId);
	
	@Select("select * from points where userId=#{userId} and point>0 and expireDate>=now() ")
	List<Point> getLegalPointsListByUserId(String userId);
	
	@Select("select * from points where userId=#{userId} and expireDate < now() order by createDate desc")
	List<Point> getIllegalPointsListByUserId(String userId);
	
	@Select("select ifnull(sum(point),0) from points where userId=#{userId} and expireDate >= now()")
	int getSumLegalPointsListByUserId(String userId);
	
	@Insert("insert into pointrollover value(null,#{userId},now(),#{amount},0)")
	int createPoint(String userId,int amount);
	
	@Insert("insert into pointrollover value(null,#{userId},now(),#{amount},1)")
	int consumePoint(String userId,int amount);
	
	@Select("select * from pointrollover where userId=#{userId} and type=0 order by date desc")
	List<PointRollover> getCreatedPointListByUserId(String userId);
	
	@Select("select * from pointrollover where userId=#{userId} and type=1 order by date desc")
	List<PointRollover> getConsumedPointListByUserId(String userId);
	
	@Select("select * from pointrollover where userId=#{userId} order by date desc")
	List<PointRollover> getPointListByUserId(String userId);
	
	@Update("Update points set point=#{point} where pointId=#{pointId}")
	int updatePointsByPointIdForConsume(int point,int pointId);
}
